/*
 * DesktopLoginBox.java
 *
 * Created on 17 de mayo de 2008, 19:59
 */
package ar.com.jpack.desktop;

import java.util.GregorianCalendar;
import java.util.TimeZone;
import javax.swing.JOptionPane;
import org.jdesktop.application.Action;

/**
 * JDialog modal que se utiliza para loguearse a la aplicacion
 * @author  jmhanun
 */
public class DesktopLoginBox extends javax.swing.JDialog {

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        usuariosT = new ar.com.jpack.transferencia.UsuariosT();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        usuarioTextField = new javax.swing.JTextField();
        contrasenaPasswordField = new javax.swing.JPasswordField();
        aceptarButton = new javax.swing.JButton();
        salirButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ar.com.jpack.desktop.DesktopApp.class).getContext().getResourceMap(DesktopLoginBox.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setModal(true);
        setName("Form"); // NOI18N
        setResizable(false);

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        usuarioTextField.setName("usuarioTextField"); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, usuariosT, org.jdesktop.beansbinding.ELProperty.create("${usuario}"), usuarioTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        contrasenaPasswordField.setName("contrasenaPasswordField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, usuariosT, org.jdesktop.beansbinding.ELProperty.create("${contrasena}"), contrasenaPasswordField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(ar.com.jpack.desktop.DesktopApp.class).getContext().getActionMap(DesktopLoginBox.class, this);
        aceptarButton.setAction(actionMap.get("validarLogin")); // NOI18N
        aceptarButton.setName("aceptarButton"); // NOI18N

        salirButton.setAction(actionMap.get("quit")); // NOI18N
        salirButton.setName("salirButton"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(usuarioTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 290, Short.MAX_VALUE)
                            .addComponent(contrasenaPasswordField, javax.swing.GroupLayout.DEFAULT_SIZE, 290, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(aceptarButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(salirButton)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {aceptarButton, salirButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(usuarioTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(contrasenaPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(salirButton)
                    .addComponent(aceptarButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton aceptarButton;
    private javax.swing.JPasswordField contrasenaPasswordField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JButton salirButton;
    private javax.swing.JTextField usuarioTextField;
    private ar.com.jpack.transferencia.UsuariosT usuariosT;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
    /** Creates new form DesktopLoginBox
     * @param parent - Frame al cual depende
     */
    public DesktopLoginBox(java.awt.Frame parent) {
        super(parent);
        initComponents();
        DesktopApp.getApplication().getDesktopView().cargaInicial();
        getRootPane().setDefaultButton(aceptarButton);
    }

    /**
     * Valida los datos ingresados en el Frame.
     * Si los datos concuerdan con algun registro en la tabla de Usuarios
     * asigna el registro en usuarioLogueado del DesktopApp.
     * En caso contrario limpia los campos y vuelve a preguntar.
     */
    @Action
    public void validarLogin() {
        if (DesktopApp.getApplication().isUsuario(usuariosT)) {
            if (DesktopApp.getApplication().getUsuarioLogueado().getIdEstado().getIdEstado() == 6) {
                DesktopApp.getApplication().getUsuarioLogueado().setUltimoAcceso(new GregorianCalendar(TimeZone.getTimeZone("America/Argentina/Cordoba")).getTime());
                DesktopApp.getApplication().updateUsuariosT(DesktopApp.getApplication().getUsuarioLogueado(), false);
                this.setVisible(false);
                DesktopApp.getApplication().getDesktopView().cargaInicial();
                this.dispose();
            } else {
                JOptionPane.showMessageDialog(this, "No esta habilitado para iniciar sesion\nConsulte al administrador");
                usuarioTextField.requestFocus();
            }
        } else {
            usuariosT.setNombres(null);
            usuariosT.setContrasena(null);
            usuarioTextField.setText("");
            contrasenaPasswordField.setText("");
            JOptionPane.showMessageDialog(this, "Usuario desconocido o contraseña incorrecta");
            usuarioTextField.requestFocus();
        }
    }
}
